﻿using Company.Core.Attributes;
using Company.Core.Enums;
using DynamicData;
using Microsoft.Xaml.Behaviors.Media;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Company.Core.Language
{
    /// <summary>
    /// 多语言具体实现子类
    /// </summary>
    [ExposedService(interfaces: typeof(ILanguageManager))]
    public class LanguageManager : ILanguageManager
    {
        private string _languagePath = string.Empty;
        private List<LanguageModel> _languages = new List<LanguageModel>();
        private ResourceDictionary _currentLanguageSource;
        public LanguageManager()
        {
            //Set(LanguageType.CN);
        }

        public string this[string key]
        {
            get
            {
                if (_currentLanguageSource != null && _currentLanguageSource.Contains(key))
                {
                    return _currentLanguageSource[key].ToString();
                }

                return null;
            }
        }

        public LanguageModel? Current { get; set; }

        public List<LanguageModel> Languages
        {
            get
            {
                if (_languages == null || _languages.Count == 0)
                {
                    _languages = GetLanguages();
                }

                return _languages;
            }
        }

        public void Set(LanguageType setLanguage)
        {
            if (Current == null || setLanguage != Current.Type)
            {
                var resource = Application.Current.Resources.MergedDictionaries[0];
                if (resource != null)
                {
                    if (Current == null) // 第2次进来的时候 resource.Source 为 null，所以这里记录下语言文件路径
                    {

                        var uri = resource.Source.AbsolutePath;
                        _languagePath = uri.Remove(uri.LastIndexOf('/'));
                    }

                    var newUri = Path.Combine(_languagePath, $"{setLanguage}.xaml");
                    _currentLanguageSource = (ResourceDictionary)Application.LoadComponent(new Uri(newUri, UriKind.RelativeOrAbsolute));
                    Application.Current.Resources.MergedDictionaries.Remove(resource);
                    Application.Current.Resources.MergedDictionaries.Insert(0, _currentLanguageSource);
                }

                if (Current == null)
                {
                    Current = new LanguageModel();
                }

                Current.Type = setLanguage;
                Current.Desc = Languages.First(d => d.Type == setLanguage).Desc;
            }
        }

        private List<LanguageModel> GetLanguages()
        {
            var list = new List<LanguageModel>();
            var fields = typeof(LanguageType).GetFields();
            foreach (var field in fields)
            {
                if (field.GetCustomAttribute<DescriptionAttribute>() is DescriptionAttribute desc)
                {
                    list.Add(new LanguageModel() { Type = (LanguageType)field.GetValue(null), Desc = desc.Description });
                }
            }

            return list;
        }

    }
}
